###
# module JsBuilder
#
module JsBuilder    
    require 'find'
    
    ###
    # load_config
    # loads config file from config/js_builder.yml
    #
    def self.load_config
        filename = 'config/js_builder.yml'
        if (!FileTest.exist?(filename)) # no yaml config detected.  just return.        
            return false
        end
        
        config = YAML.load(File.read(filename))
                            
        # load application config file
        return config            
    end
                
    ###
    # @class project
    # builds a js build project by executing Ext's java executable "builder"
    #
    class Project
                
        attr_accessor :output, :files
        
        def initialize(project)     
            
            if (!cfg = JsBuilder.load_config)
                puts "Error JsBuilder::Project -- Could not load config file"
                exit(0)
            end
                                                                                       
            if (cfg[project.to_sym].nil?)
                puts "Error JsBuild::Project -- Could not locate that project in config file"
                exit(0)
            end            
            
            # good to go...
            @config = cfg[project.to_sym]     
                        
            @name = project
            @output = ''
                                    
            @doc = Builder::XmlMarkup.new(:target => @output, :indent => 4)
            @doc.instruct!                        
        end
                        
        ###
        # build
        #
        def build
            targets = @config.delete(:targets)
            directory = @config.delete(:directory)
                        
            @config[:output] = @config[:path] + '/' + @config[:output]
            @doc.project(@config) do 
                @doc.directory(directory)
                puts "Build targets:"
                targets.each do |t|   
                    puts "  - #{t[:name]} (#{t[:file].gsub(/\$output/, @config[:output])})"
                    include = t.delete(:include)
                    @doc.target(t) do
                        include.each do |f|
                            @doc.include(:name => f)
                        end
                    end
                end                            
            end      
            
            # write .jsb file
            filename = @config[:path] + '/' + @name + ".jsb"            
            f = File.new(filename, File::CREAT|File::RDWR|File::TRUNC, 0644)
            f << @output
            f.close  
            
            # build command
            cmd = "java -jar #{File.dirname(__FILE__)}/builder/builder.jar #{RAILS_ROOT}/#{filename}"   
                                    
            puts "\n\nBuilding...please wait..."
                        
            # execute command
            puts `#{cmd}`
            
    
        end                                
    end
end